﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Catering.View_Operaction;
using System.Data;
using Microsoft.Reporting.WebForms;
using Catering_Rules;
using System.Text;

namespace Catering.DisplayBoards
{
    public partial class DietNum_rcjk1 : System.Web.UI.Page
    {
        private static DataViewForReport dvfr = new DataViewForReport();
        private static int[] RawId = { 3, 4, 6, 7, 8, 0 };//食品大类ID
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitDate();
            }
        }

        public void InitDate()
        {
            string AreaID = "1";
            string ClassID = "-10";
            DateTime DTime = Convert.ToDateTime(DateTime.Now.ToString("yyyy/MM/dd"));
            //DateTime DTime = Convert.ToDateTime(DateTime.Now.AddYears(-1).ToString("yyyy/MM/dd"));
            string TimeType = "Day";            
            string title = "";
            string banzuStr = "";
            if (TimeType == "Day")
            {
                string Str_XH = GetXiaoHaoByDay(AreaID, ClassID, DTime, RawId, out title, out banzuStr);
                if (!string.IsNullOrEmpty(Str_XH))
                {
                    ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "", "GetDataGroupByBanZu(" + DTime.Year + "," + DTime.Month + "," + DTime.Day + ",'" + title + "'," + banzuStr + "," + Str_XH + ");", true);
                }
                else
                {
                    ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "", "alert('暂无数据！');", true);
                }
            }
            else
            {
                string Str_XH = GetFactXiaoHao(AreaID, ClassID, DTime, RawId, out title);
                if (!string.IsNullOrEmpty(Str_XH))
                {
                    ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "", "GetData(" + DTime.Year + "," + DTime.Month + ",'" + title + "'," + Str_XH + ");", true);
                }
                else
                {
                    ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "", "alert('暂无数据！');", true);
                }
            }
        }
      
        public string GetFactXiaoHao(string AreaID, string ClassID, DateTime DTime, int[] RawId, out string title)
        {
            title = Bind_Data_to_Controls.Bind_ReportTitle(AreaID, ClassID, DTime, "");
            StringBuilder result = new StringBuilder("");
            string pid = "0";
            if (Session["UserID"] != null) pid = Session["UserID"].ToString();
            DataTable dt = dvfr.GetFactXiaoHao_RichangJiankong(pid, AreaID, DTime, RawId, ClassID);
            Dictionary<string, float[]> dic = new Dictionary<string, float[]>();

            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    float number = Convert.ToSingle(dt.Rows[i]["Number"]);
                    int outDay = Convert.ToInt32(dt.Rows[i]["OutDay"]);
                    float eatNumber = Convert.ToSingle(dt.Rows[i]["EatNumber"]);
                    string cName = dt.Rows[i]["CName"].ToString();
                    SetFactXiaoHaoDic(dic, number, outDay - 1, eatNumber, cName + "实际消耗", 2);
                    SetFactXiaoHaoDic(dic, number, outDay - 1, eatNumber, cName + "就餐人数", 3);
                }
                dt.Dispose();
            }           

            if (dic.Count > 0)
            {
                foreach (string key in dic.Keys)
                {
                    float[] data = dic[key];
                    result.Append("{");
                    result.Append("name:'" + key + "',");
                    result.Append("data: [");
                    for (int i = 0; i < 31; i++)
                    {
                        if (data[i] != 0)
                        {
                            result.Append(data[i].ToString("0.00"));
                        }
                        else
                        {
                            result.Append("0");
                        }
                        if (i != 30)
                        { result.Append(","); }
                    }
                    result.Append("]");
                    result.Append("},");
                }
            }
            string str = result.ToString();
            if (str.Length > 0) str = str.Substring(0, str.Length - 1);
            return str;
        }

        private static void SetFactXiaoHaoDic(Dictionary<string, float[]> dic, float number, int outDay, float eatNumber, string cName, int fun)
        {
            if (dic.ContainsKey(cName))
            {
                float[] data = new float[31];
                data = dic[cName];
                if (fun == 3)
                {
                    data[outDay] = eatNumber;
                }
                else if (fun == 2)
                {
                    if (eatNumber > 0)
                        data[outDay] = (number / eatNumber);
                    else data[outDay] = number;
                }
                else
                {
                    data[outDay] = number;
                }
                dic[cName] = data;
            }
            else
            {
                float[] data = new float[31];
                if (fun == 3)
                {
                    data[outDay] = eatNumber;
                }
                else if (fun == 2)
                {
                    if (eatNumber > 0)
                        data[outDay] = (number / eatNumber);
                    else data[outDay] = number;
                }
                else
                {
                    data[outDay] = number;
                }
                dic.Add(cName, data);
            }
        }

        public string GetXiaoHaoByDay(string StrAreaID, string StrClassID, DateTime DTime, int[] RawId, out string title, out string banzuStr)
        {
            title = Bind_Data_to_Controls.Bind_ReportTitle(StrAreaID, StrClassID, DTime, DateTime.Now, false, "", true);
            banzuStr = "";
            string result = "";
            string pid = "0";
            if (Session["UserID"] != null) pid = Session["UserID"].ToString();
            DataTable dt = dvfr.GetBanZuFactXiaoHao_RichangJiankong(pid, StrAreaID, DTime, RawId, StrClassID);
            Dictionary<int, float[]> dic = new Dictionary<int, float[]>();
            Dictionary<int, int> banzu = GetClassIDs(dt, out banzuStr);
            for (int i = 0; i < RawId.Length; i++)
            {
                dic.Add(RawId[i], new float[banzu.Count]);
            }
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    float number = Convert.ToSingle(dt.Rows[i]["Number"]);//消耗量
                    int classID = Convert.ToInt32(dt.Rows[i]["ClassID"]);//班组
                    float eatNumber = Convert.ToSingle(dt.Rows[i]["EatNumber"]);//人数
                    int rawsID = Convert.ToInt32(dt.Rows[i]["RAWZS"]);//食物种属
                    string cName = dt.Rows[i]["CName"].ToString();//班组名称

                    if (dic.ContainsKey(rawsID))
                    {
                        float[] data = new float[banzu.Count];
                        data = dic[rawsID];
                        if (banzu.ContainsKey(classID))
                        {
                            data[banzu[classID]] = float.Parse((number / eatNumber).ToString());
                        }
                        dic[rawsID] = data;
                    }
                    else
                    {
                        float[] data = new float[banzu.Count];
                        if (banzu.ContainsKey(classID))
                        {
                            data[banzu[classID]] = (number / eatNumber);
                        }
                        dic.Add(rawsID, data);
                    }
                }
                dt.Dispose();
            }
            result = GetStringByDic(dic, banzu, "实际消耗");
            dt = dvfr.GetBanZuBiaoZhunXiaoHao_RichangJiankong(StrAreaID, DTime, RawId, StrClassID);
            dic.Clear();
            for (int i = 0; i < RawId.Length; i++)
            {
                dic.Add(RawId[i], new float[banzu.Count]);
            }
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    float number = Convert.ToSingle(dt.Rows[i]["Number"]);//消耗量
                    int classID = Convert.ToInt32(dt.Rows[i]["ClassID"]);//班组
                    int rawsID = Convert.ToInt32(dt.Rows[i]["RAWZS"]);//食物种属
                    string cName = dt.Rows[i]["CName"].ToString();//班组名称

                    if (dic.ContainsKey(rawsID))
                    {
                        float[] data = new float[banzu.Count];
                        data = dic[rawsID];
                        if (banzu.ContainsKey(classID))
                        {
                            data[banzu[classID]] = number;
                        }
                        dic[rawsID] = data;
                    }
                    else
                    {
                        float[] data = new float[banzu.Count];
                        if (banzu.ContainsKey(classID))
                        {
                            data[banzu[classID]] = number;
                        }
                        dic.Add(rawsID, data);
                    }
                }
                dt.Dispose();
            }
            result += GetStringByDic(dic, banzu, "标准消耗");
            if (result.Length > 0) result = result.Substring(0, result.Length - 1);
            return result;
        }

        public Dictionary<int, int> GetClassIDs(DataTable dt, out string banzuStr)
        {
            Dictionary<int, int> ClassIDs = new Dictionary<int, int>();
            banzuStr = "[";
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    int classID = Convert.ToInt32(dt.Rows[i]["ClassID"]);//班组
                    string cName = dt.Rows[i]["CName"].ToString();//班组名称
                    if (!ClassIDs.ContainsKey(classID))
                    {
                        ClassIDs.Add(classID, ClassIDs.Count);
                        banzuStr += "'" + cName + "',";
                    }
                }
                dt.Dispose();
            }
            if (banzuStr.Length > 1) banzuStr = banzuStr.Substring(0, banzuStr.Length - 1);
            banzuStr += "]";
            return ClassIDs;
        }

        public string GetStringByDic(Dictionary<int, float[]> dic, Dictionary<int, int> banzu, string title)
        {
            StringBuilder result = new StringBuilder("");
            if (dic != null && dic.Count > 0)
            {
                foreach (int key in dic.Keys)
                {
                    string keyName = "";
                    Catering_Model.RawMaterialsType RMType = new Catering_Rules.RawMaterialsType().GetModel(key);
                    if (RMType != null && !string.IsNullOrEmpty(RMType.Name))
                    {
                        keyName = RMType.Name;
                        float[] data = dic[key];
                        result.Append("{");
                        result.Append("name:'" + keyName + title + "',");
                        result.Append("data: [");
                        for (int i = 0; i < banzu.Count; i++)
                        {
                            int tempNum = 0;
                            int.TryParse(data[i].ToString("0"), out tempNum);
                            if (data[i] != 0 && tempNum > 0)
                            {
                                result.Append(data[i].ToString("0"));
                            }
                            else
                            {
                                result.Append("0");
                            }
                            if (i != (banzu.Count - 1))
                            { result.Append(","); }
                        }
                        result.Append("]");
                        result.Append("},");
                    }

                }
            }
            return result.ToString();
        }

    }
}